angular.module('app').controller('vmUpdateOpenstackCtrl', ['$rootScope', '$scope','$state','httpLoad','$stateParams','$modal',function($rootScope, $scope,$state,httpLoad,$stateParams,$modal) {
    $rootScope.link = '/statics/css/user.css';
    $scope.param = {
        rows: 10
    };
    $scope.showDetail = 2;
    $scope.showOpenstack=1;

    var resourcetabLocation1 = sessionStorage.getItem('resourcetabLocation1');
    if(resourcetabLocation1&&resourcetabLocation1.indexOf("virtualUpdate")>=0) $rootScope.moduleTitle = '资源管理 > 主机资源';
    else $rootScope.moduleTitle = '平台管理 > OpenStack';

    $scope.goBack = function(){
        if(resourcetabLocation1&&resourcetabLocation1.indexOf("virtualUpdate")>=0){
            $state.go('app.assets.resource');
            sessionStorage.setItem('resourcetabLocation','virtualUpdate');
        }
        else {
            $state.go('app.config.openstackTab',{id:$stateParams.id});
            sessionStorage.setItem('openstackTabvendor', JSON.stringify({id:$stateParams.id}));
        }
    };

    (function(){
        $scope.updateData = {};
        //$scope.account = 'root';
        $scope.slider = {
            options: {
                showTicks: true,
                readOnly: true,
                stepsArray : [
                    {value:"1"},
                    {value:"2"},
                    {value:"4"},
                    {value:"8"},
                    {value:"16"}
                ],
                translate: function(value) {
                    return value+'核';
                },
                onChange : function(sliderId, modelValue, highValue, pointerType){
                    // var str = modelValue;
                    // $scope.updateData.cpu = str.substring(0,str.length-1);
                }
            }
        };
        //$scope.slider1 = {
        //    options: {
        //        showTicks: true,
        //        readOnly: true,
        //        stepsArray : [
        //            {value: 0.5},
        //            {value: 2},
        //            {value: 4},
        //            {value: 6},
        //            {value: 8},
        //            {value: 16}
        //        ],
        //        translate: function(value) {
        //            return value+'G';
        //        },
        //        onChange : function(sliderId, modelValue, highValue, pointerType){
        //            //var str = modelValue;
        //            //$scope.updateData.memory = str.substring(0,str.length-1);
        //        }
        //    }
        //};

        //获取可用区域数据
        $scope.getRegion = function(regionList){
            httpLoad.loadData({
                url:'/region/list',
                method: 'POST',
                data: {"id":$stateParams.id},
                success:function(data){
                    if(data.success){
                        $scope.regionData = data.data;
                        angular.forEach($scope.regionData, function(data,index){
                            data.isRegionActive = false;
                            for(var a in regionList){
                                if(regionList[a]==data.id) data.isRegionActive = true;
                            }
                        });
                    }
                }
            });
        };
        //获取镜像数据
        $scope.getImageData = function(){
            var params = {
                    simple : true
                },
                searchParam = [{"param":{"vendorId":$stateParams.id,"status":"ACTIVE"},"sign":"EQ"}];
            params.params = JSON.stringify(searchParam);
            httpLoad.loadData({
                url:'/image/list',
                method:'POST',
                data: params,
                noParam: true,
                success:function(data){
                    if(data.success){
                        $scope.imageList = data.data.rows;
                    }
                }
            });
        };
        //获取网络数据
        $scope.getNetworkData = function(networkId){
            var params = {
                    simple : true
                },
                searchParam = [{"param":{"vendorId":$stateParams.id},"sign":"EQ"}];
            params.params = JSON.stringify(searchParam);
            httpLoad.loadData({
                url:'/network/list',
                method:'POST',
                data: params,
                noParam: true,
                success:function(data){
                    if(data.success){
                        $scope.networkList = data.data.rows;
                        if((networkId+"").indexOf(",") != -1){
                            var netData = networkId.split(',');
                            angular.forEach($scope.networkList, function(data,index){
                                data.isNetworkActive = false;
                                angular.forEach(netData, function(data1,index1){
                                    if(data.value==data1) data.isNetworkActive = true;
                                });
                            });
                        }else{
                            angular.forEach($scope.networkList, function(data,index){
                                data.isNetworkActive = false;
                                if(data.value==networkId) data.isNetworkActive = true;
                            });
                        }
                    }
                }
            });
        };
        //获取配置实例数据
        $scope.getSizeData = function(){
            var params = {
                    simple : true
                },
                searchParam = [{"param":{"vendorId":$stateParams.id},"sign":"EQ"}];
            params.params = JSON.stringify(searchParam);
            httpLoad.loadData({
                url:'/flavor/list',
                method: 'POST',
                data: params,
                noParam: true,
                success:function(data){
                    if(data.success){
                        $scope.sizeList = data.data.rows;
                        $scope.sizeList.forEach(function(item){
                            if(item.flavorId==$scope.vmDetail.flavorId){
                                //选择配置实例
                                $scope.selectSize(item);
                            }
                        });
                    }
                }
            });
        };

        //获取详情数据反显
        $scope.getDetail = function(){
            var id = $stateParams.vmId;
            httpLoad.loadData({
                url:'/vm/detail',
                method:'GET',
                data: {id: id},
                success:function(data){
                    if(data.success&&data.data){
                        $scope.vmDetail = data.data;
                        var updateItem = ['id','name','region','props','account','managerIp','managerPort','vendorId','flavorId','networkId','imageId'];
                        for(var i=0;i<updateItem.length;i++){
                            var item = updateItem[i];
                            $scope.updateData[item] = $scope.vmDetail[item];
                        }
                        $scope.updateData.password = $scope.vmDetail.password;
                        var regionList = [];
                        $scope.updateData.region = $scope.updateData.region +"";
                        if($scope.updateData.region.indexOf()<0)  regionList.push($scope.updateData.region);
                        else  regionList = $scope.updateData.region.split(',');

                        //获取可用区域数据
                        $scope.getRegion(regionList);
                        //获取镜像数据
                        $scope.getImageData();
                        //获取网络数据
                        $scope.getNetworkData($scope.vmDetail.networkId);
                        //获取配置实例数据
                        $scope.getSizeData();
                    }
                }
            });
        };
        if($scope.showOpenstack==1) $scope.getDetail();

        //选择可用区域
        $scope.chooseRegion = function(item){
            angular.forEach($scope.regionData, function(data,index){
                data.isRegionActive = false;
                if(data.id==item.id){
                    data.isRegionActive = !item.isRegionActive;
                }
                if(data.isRegionActive){
                    $scope.updateData.region = data.id;
                }
            });
        };
        //选择配置实例
        $scope.selectSize = function(row){
            if(row==undefined) return;
            if(row!=""){
                var data = row;
                //$scope.updateData.flavorId = data.id;
                $scope.updateData.disk = data.disk+'';
                $scope.slider.value = data.cpu+'';$scope.updateData.cpu = data.cpu+'';
                $scope.updateData.memory = data.memory+'';
                //$scope.slider1.value = data.memory/1024;$scope.updateData.memory = data.memory;
                $scope.isselectSize = true;$scope.slider.options.readOnly = true;
                //$scope.slider1.options.readOnly = true;
            }
        };
        $scope.ok = function(){
            var param = {
                id: $scope.updateData.id,
                name: $scope.updateData.name,
                tags: $scope.updateData.tags,
                props: $scope.updateData.props
            };
            httpLoad.loadData({
                url:'/vm/modify',
                method:'POST',
                data: param,
                success:function(data){
                    if(data.success){
                        $scope.pop("虚机编辑成功");
                        $scope.goBack();
                    }
                }
            });
        };
        $scope.cancel = function(){
            $scope.goBack();
        };
    })();
}]);